package com.immortal.core.module.secure.advice;

import com.immortal.core.module.secure.anno.Decrypt;
import com.immortal.core.module.secure.anno.Secure;
import com.immortal.core.module.secure.service.DecryptHttpDecoder;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.core.MethodParameter;
import org.springframework.http.HttpInputMessage;
import org.springframework.http.converter.HttpMessageConverter;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.servlet.mvc.method.annotation.RequestBodyAdvice;

import java.io.IOException;
import java.lang.reflect.Type;

/**
 * <p> 请求数据接收处理类<br>
 * <p>对加了@Decrypt的方法的数据进行解密操作<br>
 * <p>只对@RequestBody参数有效
 *
 * @author wind
 * @version 1.0
 * @since 2018/12/20
 */
@ControllerAdvice
@Slf4j
public class EncryptRequestBodyAdvice implements RequestBodyAdvice {

    @Autowired
    private DecryptHttpDecoder decryptHttpDecoder;


    @Override
    public boolean supports(MethodParameter methodParameter, Type targetType,
                            Class<? extends HttpMessageConverter<?>> converterType) {
        return methodParameter.getMethod().getDeclaringClass().getAnnotation(Secure.class) != null
                || methodParameter.getMethod().getAnnotation(Secure.class) != null
                || methodParameter.getDeclaringClass().getAnnotation(Decrypt.class) != null
                || methodParameter.getMethod().getAnnotation(Decrypt.class) != null;
    }

    @Override
    public Object handleEmptyBody(Object body, HttpInputMessage inputMessage, MethodParameter parameter,
                                  Type targetType, Class<? extends HttpMessageConverter<?>> converterType) {
        return body;
    }

    @Override
    public HttpInputMessage beforeBodyRead(HttpInputMessage inputMessage, MethodParameter parameter, Type targetType,
                                           Class<? extends HttpMessageConverter<?>> converterType) throws IOException {
        Decrypt decrypt = null;
        Secure classSecure = parameter.getMethod().getDeclaringClass().getAnnotation(Secure.class);
        Secure secure = parameter.getMethod().getAnnotation(Secure.class);
        if (classSecure != null) {
            decrypt = classSecure.decrypt();
        }
        if (secure != null) {
            decrypt = secure.decrypt();
        }
        Decrypt classDecrypt = parameter.getMethod().getDeclaringClass().getAnnotation(Decrypt.class);
        if (classDecrypt != null) {
            decrypt = classDecrypt;
        }
        Decrypt methodDecrypt = parameter.getMethod().getAnnotation(Decrypt.class);
        if (methodDecrypt != null) {
            decrypt = methodDecrypt;
        }

        if (decrypt != null && !decrypt.ignore()) {
            return new DecryptHttpInputMessage(inputMessage, decryptHttpDecoder);
        }
        return inputMessage;
    }

    @Override
    public Object afterBodyRead(Object body, HttpInputMessage inputMessage, MethodParameter parameter, Type targetType,
                                Class<? extends HttpMessageConverter<?>> converterType) {
        return body;
    }
}

